﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using XLugia.Lib.XDataBase.Base.Model;

namespace XLugia.Lib.XDataBase.Base.Lib
{
    /// <summary>
    /// 错误信息
    /// </summary>
    public static class DatabaseException
    {
        /// <summary>
        /// 获取Sql执行出错信息
        /// </summary>
        /// <param name="id">方法ID</param>
        /// <param name="sqlScript">Sql脚本</param>
        /// <param name="exception">错误信息</param>
        /// <returns>Sql执行出错信息</returns>
        public static Exception getSqlException(string id, string sqlScript, Exception exception)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");
            sb.AppendLine("ID:" + id);
            sb.AppendLine("====================Sql Start=======================");
            sb.AppendLine(sqlScript);
            sb.AppendLine("====================Sql End=======================");
            sb.AppendLine("Error Msg：");
            sb.AppendLine(exception.GetBaseException().Message);
            sb.AppendLine("Stack：");
            sb.AppendLine(exception.GetBaseException().StackTrace);
            sb.AppendLine("Ect：");
            sb.AppendLine(exception.Message.ToString());
            sb.AppendLine(exception.Source);
            sb.AppendLine(exception.StackTrace);
            sb.AppendLine("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
            return new Exception(sb.ToString());
        }

        /// <summary>
        /// 获取Sql执行出错信息
        /// </summary>
        /// <param name="id">方法ID</param>
        /// <param name="scriptModel">DatabaseScriptModel对象</param>
        /// <param name="exception">错误信息</param>
        /// <returns>Sql执行出错信息</returns>
        public static Exception getSqlException(string id, DatabaseScriptModel scriptModel, Exception exception)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");
            sb.AppendLine("ID:" + id);
            sb.AppendLine("====================Sql Start=======================");
            sb.AppendLine(scriptModel.command);
            sb.AppendLine("====================Sql End=======================");
            sb.AppendLine("Error Msg：");
            sb.AppendLine(exception.GetBaseException().Message);
            sb.AppendLine("Stack：");
            sb.AppendLine(exception.GetBaseException().StackTrace);
            sb.AppendLine("Ect：");
            sb.AppendLine(exception.Message.ToString());
            sb.AppendLine(exception.Source);
            sb.AppendLine(exception.StackTrace);
            sb.AppendLine("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
            return new Exception(sb.ToString());
        }

        /// <summary>
        /// 获取Sql执行出错信息
        /// </summary>
        /// <param name="id">方法ID</param>
        /// <param name="scriptModels">List<DatabaseScriptModel>对象</param>
        /// <param name="exception">错误信息</param>
        /// <returns>Sql执行出错信息</returns>
        public static Exception getSqlException(string id, List<DatabaseScriptModel> scriptModels, Exception exception)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");
            sb.AppendLine("ID:" + id);
            sb.AppendLine("====================Sql Start=======================");
            foreach (var scriptModel in scriptModels)
            {
                sb.AppendLine(scriptModel.command);
            }
            sb.AppendLine("====================Sql End=======================");
            sb.AppendLine("Error Msg：");
            sb.AppendLine(exception.GetBaseException().Message);
            sb.AppendLine("Stack：");
            sb.AppendLine(exception.GetBaseException().StackTrace);
            sb.AppendLine("Ect：");
            sb.AppendLine(exception.Message.ToString());
            sb.AppendLine(exception.Source);
            sb.AppendLine(exception.StackTrace);
            sb.AppendLine("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
            return new Exception(sb.ToString());
        }

        /// <summary>
        /// 获取大批量数据插入(SQLBulk)执行出错信息
        /// </summary>
        /// <param name="id">方法Id</param>
        /// <param name="sqlDataTable">DataTable对象</param>
        /// <param name="exception">错误信息</param>
        /// <returns>Sql执行出错信息</returns>
        public static Exception getSqlException(string id, DataTable sqlDataTable, Exception exception)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼");
            sb.AppendLine("ID:" + id);
            sb.AppendLine("====================SqlDataTable Start=======================");
            if (sqlDataTable != null)
            {
                using (sqlDataTable)
                {
                    sb.AppendLine("SqlDataTable:" + sqlDataTable.TableName);
                    sb.AppendLine("");
                    foreach (DataColumn dc in sqlDataTable.Columns)
                    {
                        sb.Append(dc.ColumnName + "       ");
                    }

                    foreach (DataColumn dc in sqlDataTable.Columns)
                    {
                        sb.AppendLine("");
                        foreach (DataRow dr in sqlDataTable.Rows)
                        {
                            sb.Append(dr[dc.ColumnName].ToString() + "       ");
                        }
                    }
                }
            }
            sb.AppendLine("====================SqlDataTable End=======================");
            sb.AppendLine("Error Msg：");
            sb.AppendLine(exception.GetBaseException().Message);
            sb.AppendLine("Stack：");
            sb.AppendLine(exception.GetBaseException().StackTrace);
            sb.AppendLine("Ect：");
            sb.AppendLine(exception.Message.ToString());
            sb.AppendLine(exception.Source);
            sb.AppendLine(exception.StackTrace);
            sb.AppendLine("▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲");
            return new Exception(sb.ToString());
        }
    }
}
